package com.ld.controller;

import com.ld.annotation.LdPermission;
import com.ld.doamin.PageListQuery;
import com.ld.doamin.Role;
import com.ld.dto.RoleDTO;
import com.ld.service.IRoleService;
import com.ld.basic.utils.AjaxResult;
import com.ld.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/role")
@LdPermission(name = "角色查询类")
public class RoleController {
    @Autowired
    private IRoleService service;

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @LdPermission(name = "根据id查询角色")
    public AjaxResult getById(@PathVariable("id") Long id) {
        try {
            Role role = service.loadById(id);
            return AjaxResult.me().setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("查询失败");
        }
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    @LdPermission(name = "角色的查询所有")
    public AjaxResult getAll() {
        try {
            List<Role> roles = service.loadAll();
            return AjaxResult.me().setData(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("查询失败");
        }
    }

    /**
     * 按id删除
     */
    @DeleteMapping("/{id}")
    @LdPermission(name = "角色的按id删除")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        try {
            service.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    /**
     * 批量删除
     */
    @PatchMapping
    @LdPermission(name = "批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            service.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    /**
     * 高级查询加分页
     */
    @PostMapping
    @LdPermission(name = "高级查询")
    public AjaxResult getPage(@RequestBody PageListQuery query) {
        try {
            PageList<Role> pageList = service.queryPage(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("查询失败");
        }
    }

    /**
     * 新增/修改
     */
    @PutMapping
    @LdPermission(name = "新增或者修改")
    public AjaxResult saveRole(@RequestBody Role role){
        try {
            if (Objects.isNull(role.getId())){
                service.insert(role);
            }else {
                service.update(role);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }

    @PutMapping("/dto")
    public AjaxResult putDto(@RequestBody RoleDTO dto){

        try {
            service.putDto(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }

    @GetMapping("/getSns/{roleId}")
    public AjaxResult getSns(@PathVariable("roleId")Long roleId){
        try {
          List<String> permissionSns=  service.getSns(roleId);
            return AjaxResult.me().setData(permissionSns);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }
}
